import createRouteMap from "./create-route-map";

// 将配置路由表转化成路由映射表，同时暴露匹配、添加路由的方法
export default function createMatcher(routes) {
  // 拿到生成好的路由映射表
  const { pathMap } = createRouteMap(routes);

  // 添加多个路由
  function addRoutes(routes) {
    createRouteMap(routes, pathMap);
  }

  // 添加一个路由
  function addRoute(route) {
    createRouteMap([route], pathMap);
  }

  // 根据路径匹配对应的路由
  function match(location) {
    return pathMap[location]
  }

  return {
    addRoutes,
    addRoute,
    match
  }
}
